import django_filters
from django_filters.rest_framework import DjangoFilterBackend, FilterSet

from mysqldb.models import *


class ListDjangoFilterBackend(DjangoFilterBackend):
    """自己定义过滤器"""

    def get_schema_fields(self, view):
        if view.action not in ["list"]:
            return []
        return super().get_schema_fields(view)


class AllinpayPlaceGradeListFilterSet(FilterSet):
    """排班表帅选器"""

    pid = django_filters.NumberFilter(field_name="pid", required=False, label="场所ID")
    grade = django_filters.NumberFilter(
        field_name="grade", required=False, label="班次ID"
    )
    staff = django_filters.NumberFilter(
        field_name="staff", required=False, label="员工ID"
    )
    today = django_filters.DateFilter(field_name="today", required=False, label="日期")

    stardate = django_filters.DateFilter(
        field_name="today", required=False, method="filter_stardate", label="开始日期"
    )
    enddate = django_filters.DateFilter(
        field_name="today", required=False, method="filter_enddate", label="结束日期"
    )

    def filter_stardate(self, qs, name, value):
        lookup = "__".join([name, "gte"])
        return qs.filter(**{lookup: value})

    def filter_enddate(self, qs, name, value):
        lookup = "__".join([name, "lte"])
        return qs.filter(**{lookup: value})

    class Meta:
        model = AllinpayPlaceGradeList
        fields = ["pid", "grade", "staff", "today", "stardate", "enddate"]


class AllinpayPlaceGradeListV2FilterSet(FilterSet):
    """排班表帅选器 V2"""

    pid = django_filters.NumberFilter(lookup_expr="exact", label="场所ID")
    grade = django_filters.NumberFilter(lookup_expr="exact")
    staff = django_filters.NumberFilter(lookup_expr="exact")
    today = django_filters.DateFilter(lookup_expr="exact")
    stardate = django_filters.DateFilter(field_name="today", lookup_expr="gte")
    enddate = django_filters.DateFilter(field_name="today", lookup_expr="lte")

    class Meta:
        model = AllinpayPlaceGradeList
        fields = ["pid", "grade", "staff", "today", "stardate", "enddate"]


class AllinpayPlaceWorkitemListFilterSet(FilterSet):
    """工作项事务帅选器"""

    pid = django_filters.NumberFilter(lookup_expr="exact", label="场所ID")
    types = django_filters.NumberFilter(lookup_expr="exact", label="类型ID")
    status = django_filters.NumberFilter(lookup_expr="exact", label="状态")
    onstaff = django_filters.NumberFilter(lookup_expr="exact", label="执行人")
    stardate = django_filters.DateFilter(field_name="currentdate", lookup_expr="gte")
    enddate = django_filters.DateFilter(field_name="currentdate", lookup_expr="lte")

    class Meta:
        model = AllinpayPlaceworkitemList
        fields = ["pid", "types", "status", "stardate", "enddate", "onstaff"]
